package com.softserve.academy.food.dao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.softserve.academy.food.entity.eDishType;

import java.util.ArrayList;
import java.util.List;

@Repository
public class HibernateDishType extends Dao
{
	public ArrayList<eDishType> Create(ArrayList<eDishType> list) 
	{
		ArrayList<eDishType> save = new ArrayList<eDishType>();

		for (eDishType obj  : list)
		{
			obj.setId( (Integer) sessionFactory.getCurrentSession().save( obj ));
			save.add(obj);
		}	
		return save;
	}
	
	public eDishType Create(eDishType type) 
	{
		if (type!=null)
		{
			type.setId( (Integer)sessionFactory.getCurrentSession().save( type ) );
		}	
		return type;
	}
	
	public void Delete(int id)  
	{
		Query query;
		query = sessionFactory.getCurrentSession().createQuery("delete from eDishType where id = " + id);
		query.executeUpdate();
	}

	public void DeleteAll()  
	{
		Query query;
		query = sessionFactory.getCurrentSession().createQuery("delete from eDishType");
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	public eDishType Read(eDishType type)  
	{
		int id = type.getId();
		
		List<eDishType> list = sessionFactory.getCurrentSession().createQuery( "from eDishType where id = "+id ).list();
		
		if (list.isEmpty())
		{
			return type;
		}
		
		return list.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public eDishType Read(int id)  
	{
		eDishType type = new eDishType();
		
		List<eDishType> list = sessionFactory.getCurrentSession().createQuery("from eDishType where id = "+id).list();
		
		if (list.isEmpty())
		{
			return type;
		}
		
		return list.get(0);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<eDishType> ReadAll()  
	{
		return (ArrayList<eDishType>) sessionFactory.getCurrentSession().createQuery("from eDishType").list();
	}


}
